//
//  FHMainViewController.swift
//  FHweibo
//
//  Created by 张嵩雷 on 16/4/8.
//  Copyright © 2016年 feng. All rights reserved.
//

import UIKit

class FHMainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        //自定义tabBa
        // get only 只读属性,不能直接使用=号赋值, 使用KVC
        //   tabBar = HMMainTabBar()
        let mainTabBar = FHMainTabBar()
        // KVC赋值规则,1.找setTabBar()方法,能找到就直接调用这个方法, 2._tabBar成员变量, 3._setTabBar()方法, 4.找tabBar成员变量
        setValue(mainTabBar, forKey: "tabBar")
        
        //3(闭包使用流程).准备闭包并设置
        // 闭包调用流程1.定义闭包, 2.传递闭包
        mainTabBar.composeClosure = {
            ()->() in
            print("加号按钮被点击了")
        }
        
        
        setupChildViewControllers()
       
    }
    
    private func setupChildViewControllers() {
        //首页
        //前面的OC方法都等价于后面的swift方法
        // [UIImage imageWithNamed:] -> UIImage(named: "")
        // [类名 类名WithXXX], [类名 alloc] initWithXXX] -> 类名(xxx)
//        let homeVC = FHHomeTableViewController()
//        homeVC.title = "首页"
//        homeVC.tabBarItem.image = UIImage(named: "tabbar_home")
//        addChildViewController(UINavigationController(rootViewController: homeVC))
        
        setupChildViewController(FHHomeTableViewController(), imageName: "tabbar_home", title: "首页")
        
        
        //消息
//        let messageVC = FHMessageTableViewController()
//        messageVC.title = "消息"
//        messageVC.tabBarItem.image = UIImage(named: "tabbar_message_center")
//        addChildViewController(UINavigationController(rootViewController: messageVC))
        
            setupChildViewController(FHMessageTableViewController(), imageName: "tabbar_message_center", title: "消息")
        
        //发现
//        let discoverVC = FHDiscoverTableViewController()
//        discoverVC.title = "发现"
//        discoverVC.tabBarItem.image = UIImage(named: "tabbar_discover")
//        addChildViewController(UINavigationController(rootViewController: discoverVC))
        
        setupChildViewController(FHDiscoverTableViewController(), imageName: "tabbar_discover", title: "发现")
        
        //我
//        let profileVC = FHProfileTableViewController()
//        profileVC.title = "我"
//        profileVC.tabBarItem.image = UIImage(named: "tabbar_profile")
//        addChildViewController(UINavigationController(rootViewController: profileVC))
        setupChildViewController(FHProfileTableViewController(), imageName: "tabbar_profile", title: "我")
    }

    /// 设置子控制器
    ///
    /// - parameter controller: 控制器
    /// - parameter imageName:  图片名称
    /// - parameter title:      标题
    private func setupChildViewController(controller: UIViewController, imageName: String, title: String) {
        //设置标题
        controller.title = title
        //设置tabbar的图片
        controller.tabBarItem.image = UIImage(named: imageName)
        //设置tabbar的选中图片
        controller.tabBarItem.selectedImage = UIImage(named: imageName + "_selected")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
        
        //设置tabBarItem的title颜色
        controller.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.orangeColor()], forState: .Selected)
        
        //包装一个导航控制器,并添加到tabbarVClim
        addChildViewController(UINavigationController(rootViewController: controller))
        
    }
}
